import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xml.sax.SAXException;
/**
 * Created with IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class JetplanParseCase extends TestCase {
    private static Logger debugLogger = Logger.getLogger("forDebug");
    private static Logger errorLogger = Logger.getLogger("forError");
//    private static IFlyPlanParseController planParseControl = null;

    @Override
    protected void setUp() throws Exception {

    }

    public static void testJetplanParseCase() throws SAXException {
        // TODO Auto-generated method stub
        PropertyConfigurator.configure("log4j.properties");
        debugLogger.debug("风温接口服务已启动.....");
//        String regexRequestWind = "REQPWI/CQ\\d+/WQ(\\d+):([\\s\\S]*?)/DQ\\d+/\\w+?\\.\\w+?\\w{4}";
        String str = "201406260806,0,NN,NNNNN,JDSLFJDSLJFLDSJFLJDSJSDJFLKDSJFLJDSLFJLDSJFLS";
        // ([\\s\\S]*?)
        String regexRequest = "(\\d{12}),(\\d{1}),(\\w{2},\\w{5}),(\\w+?)";
//        String regexRequest = "(\\d{12}),(\\d{1}),(\\w{2},\\w{5}),([\\s\\S]*?)";
        Pattern pattern = Pattern.compile(regexRequest);
        Matcher matcher = pattern.matcher(str.trim()); // rawMsg

        if (matcher.matches()) {
            System.out.println(matcher.group(1) + "\n" + matcher.group(2) + "\n" + matcher.group(3) + "\n" + matcher.group(4));
//            System.out.println(matcher.group(1) + "\n" + matcher.group(2) + "\n" + matcher.group(3) + "\n" + matcher.group(4) + "\n" + matcher.group(5));
        } else {
            System.out.println("failed\n");
        }

/*
        File fBaseParam = new File(planParseControl.baseParamFilePath);
        final SAXReader readerBaseParam = new SAXReader();
        System.out.println(planParseControl.baseParamFilePath);
        System.out.println(System.getProperty("user.dir"));

        String scanDirPath = "";
        String backupDirPath = "";
        try {
            final Document paraseRulenDoc = readerBaseParam.read(fBaseParam);
            Element uniqueObject = (Element)paraseRulenDoc.selectSingleNode("/baseParamter/windTempInterfaceService/scanDir");
            scanDirPath = uniqueObject.getText();
            uniqueObject = (Element)paraseRulenDoc.selectSingleNode("/baseParamter/windTempInterfaceService/backupDir");
            backupDirPath = uniqueObject.getText();
            System.out.println("++++++++++++++");

            System.out.println(scanDirPath);
            System.out.println(backupDirPath);
        } catch (final DocumentException e) {
            // TODO Auto-generated catch block
//			e.printStackTrace();
            errorLogger.error(e.getMessage());
        }
*/


/*        // 让程序一直运行下去
//		while (true) {
            // 判断指定目录中是否存在文件
            File fSacnDir = new File(scanDirPath);
            if ((null == fSacnDir) || !fSacnDir.isDirectory()) {
                errorLogger.error("无法打开指定数据源目录");
                return ;
            }

            String[] fileNameList = fSacnDir.list();
            System.out.println( fSacnDir + "含有的文件目录总数：" + fileNameList.length );
            for(int i = 0; i < fileNameList.length; i++){
                File childFile = new File(scanDirPath + "\\" + fileNameList[i]);
                //                System.out.println(fileNameList[i] + "是否为文件：" + childFile.isFile());
                // xml格式的JetPlan数据处理
                if ( childFile.isFile() && FileUtil.IsXmlDocByContext(scanDirPath + "\\" + fileNameList[i]) ) {
                    debugLogger.debug("检测到Jetplan格式航班计划，开始解析");
                    planParseControl = new JetplanParseController();
                 // 普通文本格式的飞行计划处理
                } else if (childFile.isFile()) {
                    debugLogger.debug("检测到文本格式航班飞行计划数据，开始解析");
                    planParseControl = new CQFlyPlanParseController();
                } else {
                    errorLogger.error("数据源目录中存在无关目录");
                    continue;
                }

                boolean parseStatus = planParseControl.parseDataSource(childFile);
                if (parseStatus) {
                    debugLogger.debug("航班飞行计划数据解析完毕，开始入库");
                    planParseControl.saveToDB();
//                    childFile.delete();
                    debugLogger.debug("航班飞行计划数据入库结束，文件已删除");
                } else {
//                    System.out.println(backupDirPath);
//                    FileUtil.MoveFile(childFile, backupDirPath);
                    debugLogger.debug("本次航班飞行计划无法解析，文件" + fileNameList[i] + "已被剪切至"
                            + backupDirPath);
                }
            }
            // 每5秒钟，检测一次是否出现新的航班计划
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//		}// while*/
//        debugLogger.debug("风温接口服务已停止\n\n\n");
    }
}
